<?php
	// includes
	require_once("LoginView.php");
	require_once("LoginHandler.php");
	require_once("/Database/Database.php");
	
	/*
	 	Controller for login module
	 	Execute the flow of the Login module.
	*/
	
	class LoginController
	{ 
		/* The controllers "main" function, makes use of the View and Handler to control the flow of the execution */
		public function DoControl(Database $db)
		{
			$content = "";
			
			// Variables for the view and handler
			$loginView = new LoginView();
			$loginHandler = new LoginHandler($db);
			
			// If the user is logged in, show the logout box
			if ($loginHandler->IsLoggedIn())
			{
				// If the user klicked the logout button, log him/her out
				if ($loginView->TriedToLogOut())
					$loginHandler->DoLogout();
			}
			// If the user is logged out, show the log in window
			else
			{
				if ($loginView->TriedToLogIn())
				{
					// If the user klicked the login button
					if ($loginHandler->DoLogin($loginView->GetUserName(), $loginView->GetPassword()))
					{
						// If the suer had checked the "Remember me" check box
						if ($loginView->TriedToBeRemembered())
						{
							// Remember the user
							$loginView->RememberUser();
							
							$loginView->CheckRemembered();
						}
						// If the user does not wish to be remembered
						else
						{
							// If the user is remembered, forget him/her
							if ($loginView->IsRemembered())
								$loginView->ForgetUser();
							
							$loginView->UnCheckRemembered();
						}
					}
				}
			}
					
			// Show if the user is logged in or not
			//$content .= $loginView->DoLoginStatus($loginHandler->IsLoggedIn());
			
			$content .= $loginView->DoHeader($loginHandler->IsLoggedIn());
			
			// SHow the log in / log out box accordingly
			if ($loginHandler->IsLoggedIn())
				$content .= $loginView->DoLogoutBox();
			else
				$content .= $loginView->DoLoginBox();
			
			if ($loginView->TriedToLogIn())
				$content .= $loginView->DoLoginFeeback($loginHandler->IsLoggedIn());
				
			return $content;
		}
	}